package org.cresource.kaoshi.controller;

import io.swagger.annotations.ApiOperation;
import org.cresource.basic.util.AjaxResult;
import org.cresource.basic.util.PageList;
import org.cresource.kaoshi.domain.KS;
import org.cresource.kaoshi.domain.KS2;
import org.cresource.kaoshi.query.KSQuery;
import org.cresource.kaoshi.service.IKS2Service;
import org.cresource.kaoshi.service.IKSService;
import org.cresource.product.domain.ProductDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/kaoshi")
public class KSController {

    @Autowired
    IKSService ksService;
    @Autowired
    IKS2Service ks2Service;
    /**
     * 页面数据          直接走basic
     * 页面数据
     * 页面数据
     */
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "需要参数:currentPage和PageSize")
    public PageList<KS> queryPage(@RequestBody KSQuery ksQuery){
        return ksService.queryPage(ksQuery);
    }

    /**
     * 页面数据里面         直接走basic
     * 页面数据
     * 页面数据
     */
    @PostMapping("/detail")
    @ApiOperation(value = "分页查询",notes = "需要参数:currentPage和PageSize")
    public PageList<KS2> queryPage2(@RequestBody KSQuery ksQuery){
        return ks2Service.queryPage(ksQuery);
    }


    /**
     * 添加或者修改
     * 添加或者修改
     * 添加或者修改
     */
    @PutMapping
    @ApiOperation(value = "服务添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody KS ks){
        try {
            if(ks.getId()==null){
                ksService.add(ks);
            }else {
                ksService.update(ks);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult.me().setMessage("系统繁忙,请稍后重试");
        }
        return AjaxResult.me();
    }
    @GetMapping("/detail/{productId}")
    @ApiOperation(value = "查询一条服务数据",notes = "需要传入productId")
    public KS getDetailByProductId(@PathVariable("productId") Long productId){
        return ksService.getById(productId);
    }




    /**
     * 删除
     * 删除
     * 删除
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "服务删除",notes = "需要参数:服务的Id")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            ksService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后重试");
        }
    }


}
